<?php

namespace app\api\module;

use app\common\model\Axb;
use app\common\model\Broker;
use app\common\model\Coupon;
use app\common\model\Merch;
use app\common\model\Push;
use app\common\model\SignActivity;
use app\common\model\Store;
use app\common\model\UserCollection;
use app\common\model\UserFocus;
use app\common\model\UserLevel;
use app\common\model\UserLevelOrder;
use app\common\model\UserRechange;
use app\common\model\Users;
use app\common\model\ViewFeedbackLog;
use app\zonesss\model\Problem;
use think\facade\Db;
use WeChat\User;

class Member extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_MEMBER_APP_SMS_LOGIN,
            ZFN_MEMBER_APP_WECHAT_LOGIN,
            ZFN_MEMBER_APP_BIND_MOBILE,
            ZFN_MEMBER_APP_BIND_JPUSH,
            ZFN_MEMBER_MINI_MOBILE_LOGIN,
            ZFN_MEMBER_MINI_SMS_LOGIN,
            WJH_MEMBER_PWD_LOGIN,
            WJH_MEMBER_FORGET_PASSWORD,
            WJH_MEMBER_STORE_APPLY_SUB,
            WJH_MEMBER_PROBLEM,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】APP端短信登录
            case ZFN_MEMBER_APP_SMS_LOGIN:
                return $this->appSmsLogin();
            //【1002】APP端微信登录
            case ZFN_MEMBER_APP_WECHAT_LOGIN:
                return $this->appWechatLogin();
            //【1003】APP端微信绑定手机号码
            case ZFN_MEMBER_APP_BIND_MOBILE:
                return $this->appBindMobile();
            //【1004】APP端绑定极光
            case ZFN_MEMBER_APP_BIND_JPUSH:
                return $this->bindJpush();
            //【1005】小程序授权手机号登录
            case ZFN_MEMBER_MINI_MOBILE_LOGIN:
                return $this->miniMobileLogin();
            //【1006】小程序手机验证码登录
            case ZFN_MEMBER_MINI_SMS_LOGIN:
                return $this->miniSmsLogin();
            //【1007】签到提交
            case ZFN_MEMBER_SIGN_UP:
                return $this->signUp();
            //【1008】密码登录
            case WJH_MEMBER_PWD_LOGIN:
                return $this->pwdLogin();
            //【1009】会员中心
            case ZFN_MEMBER_CENTER:
                return $this->center();
            //【1010】个人资料
            case ZFN_MEMBER_INFO:
                return $this->info();
            //【1011】个人资料修改
            case ZFN_MEMBER_INFO_UPDATE:
                return $this->infoUpdate();
            //【1012】修改登录密码
            case ZFN_MEMBER_PWD_UPDATE:
                return $this->pwdUpdate();
            //【1013】修改手机号第一步
            case ZFN_MEMBER_MOBILE_STEP_ONE:
                return $this->mobileStepOne();
            //【1014】修改手机号第二步
            case ZFN_MEMBER_MOBILE_STEP_TWO:
                return $this->mobileStepTwo();
            //【1015】关注店铺记录
            case ZFN_MEMBER_FOCUS_LIST:
                return $this->focusList();
            //【1016】收藏商品记录
            case ZFN_MEMBER_COLLECTION_LIST:
                return $this->collectionList();
            //【1017】商家入驻信息
            case WJH_MEMBER_MERCH_APPLY_INFO:
                return $this->merchApplyInfo();
            //【1018】商家入驻提交
            case WJH_MEMBER_MERCH_APPLY_SUB:
                return $this->merchApplySub();
            //【1019】会员注销
            case ZFN_MEMBER_ACCOUNT_DESTROY:
                return $this->accountDestroy();
            //【1020】退出登录
            case ZFN_MEMBER_LOGIN_OUT:
                return $this->loginOut();
            //【1021】门店入驻信息
            case ZFN_MEMBER_STORE_APPLY_INFO:
                return $this->storeApplyInfo();
            //【1022】门店入驻提交
            case WJH_MEMBER_STORE_APPLY_SUB:
                return $this->storeApplySub();
            //【1023】意见反馈提交
            case ZFN_MEMBER_FEEDBACK_SUB:
                return $this->feedbackSub();
            //【1024】会员购买VIP信息
            case ZFN_MEMBER_VIP_INFO:
                return $this->vipInfo();
            //【1025】会员购买VIP提交
            case ZFN_MEMBER_VIP_SUB:
                return $this->vipSub();
            //【1026】会员购买VIP支付
            case ZFN_MEMBER_VIP_PAY:
                return $this->vipPay();
            //【1027】会员绑定位置
            case ZFN_MEMBER_BIND_REGION:
                return $this->bindRegion();
            //【1028】会员获取隐私号码
            case ZFN_MEMBER_MOBILE_AXB:
                return $this->mobileAxb();
            //【1029】忘记密码
            case WJH_MEMBER_FORGET_PASSWORD:
                return $this->forget_password();
            //【1030】我的优惠券
            case WJH_MEMBER_COUPON:
                return $this->member_coupon();
            //【1031】充值卡信息
            case WJH_MEMBER_RECHANGE_INFO:
                return $this->member_rechange();
            //【1032】常见问题获取
            case WJH_MEMBER_PROBLEM:
                return $this->member_problem();
            //【1033】充值卡支付
            case WJH_MEMBER_RECHANGE_PAY:
                return $this->member_rechange_pay();
            //【1034】领取优惠券
            case WJH_MEMBER_COUPON_RECEIVE:
                return $this->coupon_receive();
            //【1035】可领取的会员券数量
            case WJH_MEMBER_COUPON_RECEIVE_NUM:
                return $this->coupon_receive_num();
            default:
                send_developer_error("模块【用户】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }
    //会员获取隐私号码
    private function mobileAxb(){
        $user=$this->currentUser();
        $b_mobile=$this->essentialParam('b_mobile');
        $ob_id = Db::name('broker')->where('mobile',$b_mobile)->value("id");
        $res = Axb::addAxb(4,$ob_id,$user['mobile'],$b_mobile,3,$user['id']);
        $this->insertResponse('',$res);
        return $this->response("隐私号码");
    }

    //app短信登录
    private function appSmsLogin()
    {
        $mobile = $this->essentialParam('mobile');
        $captcha = $this->essentialParam('captcha');
        $res = Users::appSmsLogin($mobile, $captcha);
        $this->insertResponse('', $res);
        return $this->response('登录成功');
    }

    //APP微信登录
    private function appWechatLogin()
    {
        $openid = $this->essentialParam('openid');
        $nickname = $this->optionalParam('nickname');
        $sex = $this->optionalParam('sex');
        $headimgurl = $this->optionalParam('headimgurl');
        $unionid = $this->essentialParam('unionid');
        $res = Users::appWechatLogin($openid,$nickname,$sex,$headimgurl,$unionid);
        $info = $res['info'];
        unset($res['info']);
        $this->insertResponse('', $res);
        return $this->response($info);
    }

    //APP端微信绑定手机号码
    private function appBindMobile()
    {
        $temporary_id = $this->essentialParam('temporary_id');
        $mobile = $this->essentialParam('mobile');
        $captcha = $this->essentialParam('captcha');
        $res = Users::appBindMobile($temporary_id,$mobile,$captcha);
        $this->insertResponse('', $res);
        return $this->response('绑定成功！');
    }

    //登录后绑定极光
    private function bindJpush(){
        $user = $this->currentUser('true');
        $jpush_id = $this->essentialParam('jpush_id');
        Push::bindJpushId($user,$jpush_id);
        return $this->response('');
    }

    //小程序授权手机号登录
    private function miniMobileLogin()
    {
        $login_code = $this->essentialParam('login_code');
        $type = $this->essentialParam('type');
        $phone_code = $this->optionalParam('phone_code');
        $encryptedData = $this->optionalParam('encryptedData');
        $iv = $this->optionalParam('iv');
        $invite_uid = $this->optionalParam('invite_uid');
        $res = Users::miniMobileLogin($login_code,$type,$phone_code,$encryptedData,$iv,$invite_uid);
        $this->insertResponse('', $res);
        return $this->response('登录成功');
    }

    //小程序手机号验证码登录
    private function miniSmsLogin()
    {
        $login_code = $this->essentialParam('login_code');
        $mobile = $this->essentialParam('mobile');
        $captcha = $this->essentialParam('captcha');
        $invite_uid = $this->optionalParam('invite_uid');
        $res = Users::miniSmsLogin($login_code,$mobile,$captcha,$invite_uid);
        $this->insertResponse('', $res);
        return $this->response('登录成功！');
    }

    private function signUp()
    {
        $user = $this->currentUser();
        $res = SignActivity::signSub($user);
        $this->insertResponse('', $res);
        return $this->response('签到成功！');
    }

    private function pwdLogin()
    {
        $mobile = $this->essentialParam('mobile');
        $login_code = $this->essentialParam('login_code');
        $password = $this->essentialParam('password');
        $res = Users::pwdLogin($mobile,$password,$login_code);
        $this->insertResponse('', $res);
        return $this->response('登录成功');
    }

    private function center()
    {
        $user = $this->currentUser(false);
        $data = Users::center($user);
        $this->insertResponse('', $data);
        return $this->response('会员中心');
    }

    private function info()
    {
        $user = $this->currentUser();
        $data = Users::info($user);
        $this->insertResponse('', $data);
        return $this->response('个人资料');
    }

    private function infoUpdate()
    {
        $user = $this->currentUser();
        $type = $this->essentialParam('type');
        $value = $this->essentialParam('value');
        Users::infoUpdate($user, $type, $value);
        return $this->response('修改成功');
    }

    private function pwdUpdate()
    {
        $user = $this->currentUser();
        $mobile = $this->essentialParam('mobile');
        $captcha = $this->essentialParam('captcha');
        $newpwd = $this->essentialParam('newpwd');
        $conpwd = $this->essentialParam('conpwd');
        Users::pwdUpdate($user, $mobile, $captcha,$newpwd,$conpwd);
        return $this->response('修改成功');
    }
    private function forget_password()
    {
        $account = $this->essentialParam('account');
        $mobile = $this->essentialParam('mobile');
        $captcha = $this->essentialParam('captcha');
        $newpwd = $this->essentialParam('newpwd');
        $conpwd = $this->essentialParam('conpwd');
        Users::forgetpwd( $mobile, $captcha,$newpwd,$conpwd,$account);
        return $this->response('修改成功');
    }

    private function mobileStepOne()
    {
        $user = $this->currentUser();
        $old_mobile = $this->essentialParam('old_mobile');
        $old_captcha = $this->essentialParam('old_captcha');
        $data = Users::mobileStepOne($user, $old_mobile, $old_captcha);
        $this->insertResponse('',$data);
        return $this->response('验证成功');
    }

    private function mobileStepTwo()
    {
        $user = $this->currentUser();
        $mobile_verify = $this->essentialParam('mobile_verify');
        $new_mobile = $this->essentialParam('new_mobile');
        $new_captcha = $this->essentialParam('new_captcha');
        Users::mobileStepTwo($user,$mobile_verify, $new_mobile, $new_captcha);
        return $this->response('修改成功');
    }

    private function focusList()
    {
        $user = $this->currentUser();
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = UserFocus::userFocusList($user,$page,$page_size);
        $this->insertResponse('',$data);
        return $this->response('关注店铺记录');
    }

    private function collectionList()
    {
        $user = $this->currentUser();
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = UserCollection::userCollectionList($user,$page,$page_size);
        $this->insertResponse('',$data);
        return $this->response('收藏商品记录');
    }

    private function merchApplyInfo()
    {
        $user = $this->currentUser();
        $data =Users::getsupinfo($user);
        $this->insertResponse('',$data);
        return $this->response('超市入驻信息');
    }

    private function merchApplySub()
    {
        $user = $this->currentUser();
        $data = [
            'id' => $user['id'],
            'title' => $this->essentialParam('title'),
            'mobile' => $this->essentialParam('mobile'),
            'account' => $this->essentialParam('account'),
            'nickname' => $this->essentialParam('nickname'),
            'username' => $this->essentialParam('username'),
            'pro_id' => $this->essentialParam('pro_id'),
            'city_id' => $this->essentialParam('city_id'),
            'area_id' => $this->essentialParam('area_id'),
            'address' => $this->essentialParam('address'),
            'license' => $this->essentialParam('license'),
            'door_photo' => $this->essentialParam('door_photo'),
            'store_photo' => $this->essentialParam('store_photo'),
            'cashier_photo' => $this->essentialParam('cashier_photo'),
            'idcardfront' => $this->essentialParam('idcardfront'),
            'idcardopposite' => $this->essentialParam('idcardopposite'),

        ];
        Users::supupdate($data);
        return $this->response('修改成功');
    }

    private function accountDestroy()
    {
        $user = $this->currentUser();
        Users::destroyUser($user);
        return $this->response('账号已注销');
    }

    private function loginOut()
    {
        $user = $this->currentUser();
        Users::loginOut($user);
        return $this->response('退出成功');
    }


    private function storeApplyInfo()
    {
        $user = $this->currentUser();
        $data = Store::applyInfo($user);
        $this->insertResponse('',$data);
        return $this->response('门店入驻信息');
    }

    private function storeApplySub()
    {
        $data = [
            'title' => $this->essentialParam('title'),//门店名称
            'mobile' => $this->essentialParam('mobile'),//手机号
            'username' => $this->essentialParam('username'),//姓名
            'account' => $this->essentialParam('account'),//用户名
            'nickname' => $this->essentialParam('nickname'),//超市名称
            'password' => $this->essentialParam('password'),//登录密码
            'pro_id' => $this->essentialParam('pro_id'),//省id
            'city_id' => $this->essentialParam('city_id'),//城市id
            'area_id' => $this->essentialParam('area_id'),//县区id
            'longitude' => $this->essentialParam('longitude'),
            'latitude' => $this->essentialParam('latitude'),
            'address' => $this->essentialParam('address'),//详细地址
            'license' => $this->essentialParam('license'),//营业执照
            'door_photo' => $this->essentialParam('door_photo'),//门头照
            'store_photo' => $this->essentialParam('store_photo'),//店内照
            'cashier_photo' => $this->essentialParam('cashier_photo'),//收银台照
            'idcardfront' => $this->essentialParam('idcardfront'),//身份证正面
            'idcardopposite' => $this->essentialParam('idcardopposite'),//身份证反面
        ];
        Users::supInsert($data);
        return $this->response('入驻申请已提交，请耐心等待审核');
    }

    private function feedbackSub()
    {
        $user = $this->currentUser();
        $content = $this->essentialParam('content');
        $imgs = $this->optionalParam('imgs');
        ViewFeedbackLog::sub($user,$content,$imgs);
        return $this->response('提交成功');
    }

    private function vipInfo()
    {
        $user = $this->currentUser();
        $data = UserLevel::vipPage($user);
        $this->insertResponse('',$data);
        return $this->response('会员购买VIP信息');
    }

    private function vipSub()
    {
        $user = $this->currentUser();
        $source = $this->essentialParam('source');
        $data = UserLevelOrder::vipSub($user,$source);
        $this->insertResponse('',$data);
        return $this->response('会员购买VIP下单成功');
    }

    private function vipPay()
    {
        $user = $this->currentUser();
        $order_sn = $this->essentialParam('order_sn');
        $pay_type = $this->essentialParam('pay_type');
        $source = $this->essentialParam('source');
        $data = UserLevelOrder::vipPay($user,$order_sn,$pay_type,$source);
        $this->insertResponse('',$data);
        return $this->response('会员购买VIP支付信息');
    }

    private function bindRegion()
    {
        $user = $this->currentUser();
        $pro_id = $this->optionalParam('pro_id');
        $city_id = $this->optionalParam('city_id');
        $area_id = $this->optionalParam('area_id');
        Users::regionBind($user,$pro_id,$city_id,$area_id);
        return $this->response('确认成功');
    }
    private function member_coupon()
    {


        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $user = $this->currentUser();
        $data=Coupon::getcoupon($user['id'],$page,$page_size);
        $this->insertResponse('',$data);

        return $this->response('获取成功');
    }
    private function member_rechange()
    {
        $user = $this->currentUser();
        $data=UserRechange::getrechange($user);
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
    private function member_rechange_pay()
    {
        $user = $this->currentUser();
        $id = $this->essentialParam('id');
        $pay_type = $this->essentialParam('pay_type');
        $data=UserRechange::rechangepay($id,$user,$pay_type);
        $this->insertResponse('',$data);
        return $this->response('充值成功');
    }
    private function member_problem()
    {
        $data=Problem::getproblem();
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
    private function coupon_receive()
    {
        $user = $this->currentUser();
        Coupon::getreceivecoupon($user['id']);

        return $this->response('领取成功');
    }
    private function coupon_receive_num()
    {
        $user = $this->currentUser();
        $data=Coupon::getreceivecouponcount($user['id']);
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
}